import BaseSprite from "../../../Script/Base/BaseSprite";
import { EResType } from "../../../Script/Common/Enum";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { IFlyResData } from "../../../Script/Interface/IMessage";
import { SceneGame } from "../../../Script/SceneGame";
import GameUtil from "../../../Script/Util/GameUtil";
import { LoadUtil } from "../../../Script/Util/LoadUtil";
import MarketUI, { IMarketItem } from "./MarketUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MarketGuest extends BaseSprite {

    @property({ type: cc.Node, tooltip: "人物" })
    private skes: cc.Node = null;

    @property({ type: cc.Node, tooltip: "气泡" })
    private bubble: cc.Node = null;
    @property({ type: cc.ProgressBar, tooltip: "进度条" })
    private progress: cc.ProgressBar = null;
    @property({ type: cc.Sprite, tooltip: "icon" })
    private icon: cc.Sprite = null;

    private ske: sp.Skeleton = null;

    private data: IMarketItem = null;
    private isUpdate: boolean = false;
    private isLeftDir: boolean = false;

    private showSkeleton() {
        this.isLeftDir = Math.random() > 0.5;
        this.skes.children.forEach((o) => {
            o.active = false;
        });
        const node = this.skes.children[Math.floor(Math.random() * this.skes.children.length)];
        this.ske = node.getComponent(sp.Skeleton);
        node.active = true;
        node.scaleX = this.isLeftDir ? 0.7 : -0.7;
        GameUtil.playAnimation({ ske: this.ske, animName: 'zoulu', isLoop: true });
    }

    public come() {
        this.showSkeleton();

        const comeX = this.isLeftDir ? -GlobalData.stage_width : GlobalData.stage_width;
        const stopX = GameUtil.getRandomInteger(-200, 240);;
        const time = this.isLeftDir ? stopX - comeX : comeX - stopX;
        this.node.x = comeX;
        this.node.y = GameUtil.getRandomInteger(-50, -200);
        this.node.zIndex = 1000 - Math.floor(this.node.y);

        this.bubble.active = false;
        this.progress.progress = 0;
        this.node.stopAllActions();
        cc.tween(this.node).sequence(
            cc.moveTo(time / 1000 * 3, cc.v2(stopX, this.node.y)),
            cc.callFunc(() => {
                const isStroll = Math.random() > 0.6;
                this.data = MarketUI.instance.getGuestNeedData();
                if (this.data && !isStroll) {
                    this.isUpdate = true;
                    this.bubble.active = true;
                    // 更新icon
                    LoadUtil.instance.setSpriteFrame(this.icon, EResType.Item, this.data.itemId, 80, 80);
                    // 待机
                    GameUtil.playAnimation({ ske: this.ske, animName: 'daiji', isLoop: true });
                } else {
                    this.go();
                }
            }),
        ).start();
    }

    private go() {
        if (this.ske.animation !== 'zoulu') {
            GameUtil.playAnimation({ ske: this.ske, animName: 'zoulu', isLoop: true });
        }

        const stopX = this.isLeftDir ? GlobalData.stage_width : -GlobalData.stage_width;
        const time = this.isLeftDir ? stopX - this.node.x : this.node.x - stopX;

        this.node.stopAllActions();
        cc.tween(this.node).sequence(
            cc.moveTo(time / 1000 * 3, cc.v2(stopX, this.node.y)),
            cc.callFunc(() => {
                MarketUI.instance.guestCount--;
                this.node.destroy();
            }),
        ).start();
    }

    protected update(dt: number) {
        if (!this.isUpdate) return;

        this.progress.progress += dt / 3;  // 3s
        if (this.progress.progress >= 1) {
            this.isUpdate = false;
            this.bubble.active = false;
            this.go();
        }
    }

    private onBtnClicked(event, eventData) {
        if (this.bubble.active) {
            // 卖出金额 = 基础金额 + 基础金额 / 2 * 加价次数
            const money = this.data.sale + this.data.sale / 2 * MarketUI.instance.addCount;
            const data = SceneGame.instance.getFlyResData();
            const flyData: IFlyResData = {
                resType: EResType.Money,
                parent: data.flyNode,
                from: this.bubble,
                to: data.money,
                value: Math.ceil(money),
                flyTimes: 10,
                maxWidth: 60,
                maxHeight: 60
            }
            GameUtil.instance.flyResIcon(flyData);
            MarketUI.instance.refreshStall(this.data.itemId);

            this.bubble.active = false;
            this.go();
        }
    }
}